﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;
using ASPNETVN.PORTAL.Components.UI;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class CooperativeSearch : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                ddlArea.DataSource = Area.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlArea.DataBind();

                ddlCareer.DataSource = Career.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlCareer.DataBind();

                ddlType.DataSource = Components.Type.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlType.DataBind();

                if (Request.QueryString["careerid"] != null)
                {
                    // Hiển thị theo ngành nghề.
                    ddlCareer.SelectedValue = Request.QueryString["careerid"];
                }
                else if (Request.QueryString["areaid"] != null)
                {
                    // Hiển thị theo địa bàn.
                    ddlArea.SelectedValue = Request.QueryString["areaid"];
                }
                else if (Request.QueryString["typeid"] != null)
                {
                    // Hiển thị theo loại hình.
                    ddlType.SelectedValue = Request.QueryString["typeid"];
                }

                this.Search();

            }
        }

        protected void gvCooperative_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCooperative.PageIndex = e.NewPageIndex;
            if (ViewState["CooperativeDataSet"] != null)
            {
                gvCooperative.DataSource = ViewState["CooperativeDataSet"];
                gvCooperative.DataBind();
            }
            else
            {
                this.Search();
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Search();



            //const string commandText = "SELECT * FROM t_COOPERATIVE_Cooperatives";
            //SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            //SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);
            //DataSet ds = db.ExecuteDataSet(dbCommand);
            //foreach (DataRow row in ds.Tables[0].Rows)
            //{
            //    int id = Convert.ToInt32(row["ID"].ToString());

            //    Cooperative cooperative = Cooperative.Load(id);
            //    cooperative.Title = HttpUtility.HtmlDecode(row["Title"].ToString());
            //    cooperative.Address = HttpUtility.HtmlDecode(row["Address"].ToString());
            //    cooperative.Update();
            //}
        }

        private void Search()
        {
            string value = txtSearchValue.Text.Trim();
            string where = string.Format("CO.Title LIKE N'%{0}%' AND CO.AreaID LIKE '{1}' AND CO.CareerID LIKE '{2}' AND CO.TypeID LIKE '{3}'", value, ddlArea.SelectedValue, ddlCareer.SelectedValue, ddlType.SelectedValue);
            ViewState["CooperativeDataSet"] = Cooperative.Search(this.ModuleID, where);
            gvCooperative.DataSource = ViewState["CooperativeDataSet"];
            gvCooperative.PageIndex = 0;
            gvCooperative.SelectedIndex = -1;
            gvCooperative.DataBind();
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    const string commandText = "SELECT * FROM KTT ORDER BY mktt";
        //    SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("HTX");

        //    SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(commandText);

        //    DataSet ds = db.ExecuteDataSet(dbCommand);
        //    int i = 76;
        //    foreach (DataRow row in ds.Tables[0].Rows)
        //    {
        //        row["mktt"] = i++;
        //        row["Tenktt"] = HttpUtility.HtmlDecode(row["Tenktt"].ToString());
        //        row["Tdcm"] = HttpUtility.HtmlDecode(row["Tdcm"].ToString());
        //        if (row["Tdcm"].ToString().ToLower().Equals("đại học"))
        //        {
        //            row["Tdcm"] = 1;
        //        }
        //        else if (row["Tdcm"].ToString().ToLower().Equals("ptth"))
        //        {
        //            row["Tdcm"] = 4;
        //        }
        //        else if (row["Tdcm"].ToString().ToLower().Equals("trung cấp"))
        //        {
        //            row["Tdcm"] = 3;
        //        }
        //        row["madt"] = 4;

        //        Employee employee = new Employee();
        //        employee.ID = Convert.ToInt32(row["mktt"].ToString());
        //        employee.FullName = row["Tenktt"].ToString();
        //        employee.JobTitle = "KTT";
        //        employee.TrainingTypeID = 4;
        //        employee.TrainingLevelID = Convert.ToInt32(row["Tdcm"].ToString());
        //        employee.AppointingYear = Convert.ToInt32(row["Nam"].ToString());
        //        employee.ModuleID = new Guid("1042ac9f-d27b-400d-9663-fe4bf1b33645");
        //        employee.Insert1();
        //    }

        //    GridView1.DataSource = ds;
        //    GridView1.DataBind();
        //}
    }
}